Skip to content

CSE Machine : Fix Highlighting#3690

Merged
RichDom2185 merged 11 commits into
source-academy:masterfrom
Akshay-2007-1:fix-cse
Mar 26, 2026
Merged

CSE Machine : Fix Highlighting#3690
RichDom2185 merged 11 commits into
source-academy:masterfrom
Akshay-2007-1:fix-cse

Conversation

@Akshay-2007-1

@Akshay-2007-1 Akshay-2007-1 commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Description

Appends an additional minor functionality where on hover or on click, instead of just highlighting the arrow, we also highlight its immediate source. Other possible variations where we highlight the entire recursive backtracked path from the Global frame has been discussed but discarded in the comments of #3683 because it may cause confusion for the users.

An additional point being that it also highlights a dead arrow on clicking since its a conscious choice by the user.

Closes #3683.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Code quality improvements

How to test

Any code on running with a valid CSE Machine diagram can be used to hover over arrows or clicked upon. Some excerpts through pictures are shown below :

Image Image

Checklist

  • I have tested this code
  • I have updated the documentation

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the user experience of the CSE Machine visualization by enhancing the arrow highlighting mechanism. Instead of only highlighting the arrow itself, the system now also highlights the immediate source element of the arrow upon hover or click. This change provides more intuitive visual feedback, helping users better understand the connections and flow within the diagram without introducing the complexity of recursive path highlighting.

Highlights

  • Enhanced Arrow Highlighting: Implemented functionality to highlight the immediate source of an arrow in the CSE Machine diagram when the arrow is hovered over or clicked, providing clearer visual context.
  • New Interface Methods: Introduced setArrowSourceHighlightedStyle and setArrowSourceNormalStyle methods to the IVisible interface and Visible base class, allowing various components to define their highlighting behavior.
  • Component-Specific Highlighting: Added specific implementations for arrow source highlighting and normalization across ArrayUnit, ControlItemComponent, Frame, StashItemComponent, Text, ContValue, FnValue, and GlobalFnValue components.
  • Arrow Deselection: Modified GenericArrow to deselect and revert an arrow to its normal style if it is clicked again while already selected, improving interactive control.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a nice enhancement to the CSE machine visualization, highlighting the source of an arrow when it's hovered or clicked. The implementation is spread across multiple components, correctly adding optional methods to the IVisible interface and implementing them where needed. The change to allow toggling arrow selection on click is also a good usability improvement.

My review has identified several instances of code duplication in the new style handling methods. While the logic is correct, extracting the duplicated code into helper methods would improve maintainability. I've left specific comments with suggestions on how to refactor this.

Comment thread src/features/cseMachine/components/ControlItemComponent.tsx
Comment thread src/features/cseMachine/components/StashItemComponent.tsx
Comment thread src/features/cseMachine/components/values/ContValue.tsx
Comment thread src/features/cseMachine/components/values/FnValue.tsx
Comment thread src/features/cseMachine/components/values/GlobalFnValue.tsx
@Akshay-2007-1

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a useful feature for highlighting the source of an arrow upon hover or click, enhancing the CSE Machine's interactivity. The implementation is well-structured across various components. My review focuses on a couple of areas where code quality and maintainability can be improved by refactoring to reduce duplication and enhance property access patterns.

Comment thread src/features/cseMachine/components/Visible.tsx Outdated
Comment thread src/features/cseMachine/components/ArrayUnit.tsx

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Enhances CSE Machine arrow interactions so that hovering/clicking an arrow also highlights the arrow’s immediate source (e.g., frame/value/control/stash item), improving diagram readability for students.

Changes:

  • Add source-highlight hooks (setArrowSourceHighlightedStyle / setArrowSourceNormalStyle) across key CSE diagram nodes (frames, values, stack items, etc.).
  • Update GenericArrow to apply/revert source highlighting on hover/selection, including click-to-toggle selection behavior.
  • Introduce shared styling helpers in Visible to support applying highlight/normal styles to component shapes.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/features/cseMachine/components/values/GlobalFnValue.tsx Adds arrow-source highlight/normal styling for global function values.
src/features/cseMachine/components/values/FnValue.tsx Adds arrow-source highlight/normal styling for non-global function values.
src/features/cseMachine/components/values/ContValue.tsx Adds arrow-source highlight/normal styling for continuation values.
src/features/cseMachine/components/arrows/GenericArrow.tsx Invokes source highlight/normal styling and supports deselect-to-normal on click.
src/features/cseMachine/components/Visible.tsx Adds shared setShapesStyle helper and base no-op arrow-source style hooks.
src/features/cseMachine/components/Text.tsx Implements arrow-source highlight/normal styling for text nodes.
src/features/cseMachine/components/StashItemComponent.tsx Implements arrow-source highlight/normal styling for stash items.
src/features/cseMachine/components/Frame.tsx Implements arrow-source highlight/normal styling for frames (rect + title).
src/features/cseMachine/components/ControlItemComponent.tsx Implements arrow-source highlight/normal styling for control items.
src/features/cseMachine/components/ArrayUnit.tsx Implements arrow-source highlight/normal styling for array units.
src/features/cseMachine/CseMachineTypes.ts Extends IVisible with optional arrow-source highlight/normal hooks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/features/cseMachine/components/Visible.tsx Outdated
Comment thread src/features/cseMachine/components/Visible.tsx Outdated
Comment thread src/features/cseMachine/components/Frame.tsx Outdated
Comment thread src/features/cseMachine/components/Visible.tsx Outdated
@martin-henz martin-henz removed their request for review March 24, 2026 10:24
Comment thread src/features/cseMachine/components/arrows/GenericArrow.tsx
@Akshay-2007-1

Copy link
Copy Markdown
Contributor Author

In the latest commit, I have included all the suggestions @martin-henz has given us here


image

As in the image, it features a seperate color for objects and arrows that are highlighted due to being selected or hovered on based on whether they are live or dead. Another thing to add is that, now the target of the arrow along with the source is highlighted as well!

Another nuance that should be observed is that if an arrow and its source are dead, it is not necessary that the target is dead. In those cases, when hovered on, the arrow and the source will be highlighted in a duller tone as compared to the target which is very much alive haha!

@Akshay-2007-1

Copy link
Copy Markdown
Contributor Author

/gemini review

The code segment has already been reviewed multiple times and this is potentially the last time due to the newer additions
@gemini-code-assist

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces new styling capabilities for the CSE machine visualization, allowing elements to display different highlighted and normal styles based on their 'live' or 'dead' state. This is achieved by adding new color configurations, extending the IVisible interface with setArrowSourceHighlightedStyle and setArrowSourceNormalStyle methods, and implementing these methods across various UI components like ArrayUnit, Frame, Text, and different Value types. The GenericArrow component now utilizes these new methods to update the styles of its source and target elements. Feedback suggests improving type safety in Visible.tsx by explicitly typing Konva shapes and addressing a potentially redundant else block in GenericArrow.tsx's onClick method.

Comment thread src/features/cseMachine/components/Visible.tsx
Comment thread src/features/cseMachine/components/arrows/GenericArrow.tsx Outdated
Comment on lines 203 to +207
this.arrowHeadRef.current.pointerWidth(Config.ArrowHeadSize);
this.arrowHeadRef.current.pointerLength(Config.ArrowHeadSize);
}
this.source.setArrowSourceNormalStyle?.();
this.target?.setArrowSourceNormalStyle?.();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Hovering away from an unselected arrow can incorrectly remove the highlight from a shared node that is part of a separate, selected arrow.
Severity: CRITICAL

Suggested Fix

Update the setNormalStyle() and setArrowSourceNormalStyle() methods. Before resetting a node's style, they should check the global selection state to verify if the node is part of any currently selected arrow. If it is, the highlight style should be maintained instead of being reset.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/features/cseMachine/components/arrows/GenericArrow.tsx#L203-L207

Potential issue: When an arrow is selected, its source and target nodes are highlighted.
If the user then hovers over and away from a different, unselected arrow that shares a
source or target node, the shared node's highlight is incorrectly removed. This occurs
because the `onMouseLeave` event on the unselected arrow triggers `setNormalStyle()`,
which unconditionally resets the node's style via `setArrowSourceNormalStyle?.()`. This
reset logic is unaware that the node should remain highlighted because it is part of
another arrow that is still selected, leading to incorrect visual feedback for the user.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc_2026-03-26_14-15-48.mp4

This is a real issue!
@gemini-code-assist how can we fix this simply

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are absolutely right, this is a critical issue that needs to be addressed. The bug occurs because setArrowSourceNormalStyle() is called unconditionally on onMouseLeave for an unselected arrow, which can inadvertently remove the highlight from a shared node that is still part of a selected arrow. This leads to incorrect visual feedback.

To fix this simply, we need to ensure that a node's style is only reset if it is not currently highlighted by the globally selected arrow. This requires a two-part change:

  1. Add a helper method to ArrowSelectionManager: This method will check if a given IVisible node is the source or target of the currently selected arrow. You can add this method to src/features/cseMachine/components/arrows/ArrowSelection.ts:

    // In src/features/cseMachine/components/arrows/ArrowSelection.ts
    // Inside the ArrowSelectionManager class
    isNodeHighlightedBySelectedArrow(node: IVisible): boolean {
      if (!this.selectedArrow) {
        return false;
      }
      return this.selectedArrow.source === node || this.selectedArrow.target === node;
    }
  2. Modify GenericArrow.setNormalStyle(): Update the setNormalStyle method in src/features/cseMachine/components/arrows/GenericArrow.tsx to use this new helper method. This will prevent the source or target node's style from being reset if it's part of the currently selected arrow.

I've provided a code suggestion below for the change in GenericArrow.tsx that directly addresses the lines in this comment thread.

Suggested change
this.arrowHeadRef.current.pointerWidth(Config.ArrowHeadSize);
this.arrowHeadRef.current.pointerLength(Config.ArrowHeadSize);
}
this.source.setArrowSourceNormalStyle?.();
this.target?.setArrowSourceNormalStyle?.();
this.arrowHeadRef.current.pointerWidth(Config.ArrowHeadSize);
this.arrowHeadRef.current.pointerLength(Config.ArrowHeadSize);
}
// Only reset source/target style if they are not part of the currently selected arrow
if (this.source && !arrowSelection.isNodeHighlightedBySelectedArrow(this.source)) {
this.source.setArrowSourceNormalStyle?.();
}
if (this.target && !arrowSelection.isNodeHighlightedBySelectedArrow(this.target)) {
this.target.setArrowSourceNormalStyle?.();
}

@Akshay-2007-1 Akshay-2007-1 Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martin-henz @RichDom2185 @Oofky
I would appreciate your help and suggestions as to if this is necessary or we can ignore it since its a minor feature?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, feel free to ignore. You may want to add a comment why.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, can ignore from this PR. But please create an issue that references this comment thread.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created it: #3696

@RichDom2185 RichDom2185 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks a lot!

@RichDom2185 RichDom2185 merged commit 6554b34 into source-academy:master Mar 26, 2026
7 checks passed
@Akshay-2007-1 Akshay-2007-1 deleted the fix-cse branch March 26, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nice-to-have Issue is a non-critical, non-important

Development

Successfully merging this pull request may close these issues.

CSE Machine: Highlighting frames alongside the arrow

6 participants